{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python Class used in harvesting and processing data collected and retrived from `Twitter`, `Github` and `Meetup`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pymongo import MongoClient as MCli"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class IO_Mongo(object):\n",
    "    \"\"\"Connect to the mongo server on localhost at port 27017.\"\"\"\n",
    "    conn={'host':'localhost', 'ip':'27017'}\n",
    "\n",
    "\n",
    "    # Initialize the class with client connection, the database (i.e. twtr_db), and the collection (i.e. twtr_coll)\n",
    "    def __init__(self, db='twtr_db', coll='twtr_coll', **conn):\n",
    "        \"\"\"Connect to the MonfoDB server\"\"\"\n",
    "        self.client = MCli(**conn)\n",
    "        self.db = self.client[db]\n",
    "        self.coll = self.db[coll]\n",
    "\n",
    "\n",
    "    # The `save` method inserts new records in the pre_initialized collection and database\n",
    "    def save(self, data):\n",
    "        \"\"\" Insert data to collection in db. \"\"\"\n",
    "        return self.coll.insert(data)\n",
    "    \n",
    "    \n",
    "    # The `load` method allows the retrieval of specific records\n",
    "    def load(self, return_cursor=False, criteria=None, projection=None):\n",
    "        \"\"\" The `load` method allows the retrieval of specific records according to criteria and projection. \n",
    "            In case of large amount of data, it returns a cursor.\n",
    "        \"\"\"\n",
    "        if criteria is None:\n",
    "            criteria = {}\n",
    "        \n",
    "        # Find record according to some criteria.\n",
    "        if projection is None:\n",
    "            cursor = self.coll.find(criteria)\n",
    "        else:\n",
    "            cursor = self.coll.find(criteria, projection)\n",
    "        \n",
    "        # Return a cursor for large amount of data\n",
    "        if return_cursor:\n",
    "            return cursor\n",
    "        else:\n",
    "            return [item for item in cursor]\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = IO_Mongo()\n",
    "f.load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
